package persistencia;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

import entidades.Partida;
import entidades.Player;

public class InteraccionesBD {

	public static void insertMatch(Statement s, long matchID, long gameMode,
			long radiantWin, long duration) {
		try {
			s.executeUpdate("INSERT INTO matches " + "VALUES (" + matchID
					+ ", " + gameMode + ", " + radiantWin + "," + duration
					+ ")");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void insertMatch(Statement s, Partida p, Long playerID) {
		try {
			// Insertar la partida
			s.executeUpdate("INSERT INTO matches " + "VALUES (" + playerID + ","
					+ p.getMatchID() + ", " + p.getGameMode() + ", "
					+ p.getRadiantWin() + "," + p.getDuration() + ")");

			// Insertar player
			for (Player pl : p.getPlayers()) {
				insertaplayer(s, pl, p.getMatchID());
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void insertaplayer(Statement s, Player p, Long matchID) {
		try {
			s.executeUpdate("INSERT INTO playersdatabymatch " + "VALUES (" + matchID
					+ ", " + p.getPlayerID() + ", " + p.getHeroID() + ","
					+ +p.getItem0() + "," + p.getItem1() + "," + p.getItem2()
					+ "," + p.getItem3() + "," + p.getItem4() + ","
					+ p.getItem5() + "," + p.getKills() + "," + p.getDeaths()
					+ "," + p.getAssists() + "," + p.getGold() + ","
					+ p.getLastHits() + "," + p.getDenies() + ","
					+ p.getGold_per_min() + "," + p.getXp_per_min() + ","
					+ p.getLevel() + "," + p.getSlot() + ")");
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println(p.getPlayerID());
		}
	}

	public static void subirHeroes(Statement s,
			HashMap<Long, ArrayList<String>> heroes) {

		try {

			for (Long heroID : heroes.keySet()) {
				String query = "INSERT INTO heroes " + " VALUES (" + heroID
						+ ", \"" + heroes.get(heroID).get(1) + "\", \""
						+ heroes.get(heroID).get(0) + "\", NULL, NULL)";
				System.out.println(query);
				s.executeUpdate(query);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void subirItems(Statement s,
			HashMap<Long, ArrayList<String>> items) {

		try {

			for (Long itemID : items.keySet()) {
				String query = "INSERT INTO items " + " VALUES (" + itemID
						+ ", \"" + items.get(itemID).get(0) + "\", \""
						+ items.get(itemID).get(1) + "\")";
				System.out.println(query);
				s.executeUpdate(query);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void borrarDatos(Statement s) {
		try {
			s.executeUpdate("SET FOREIGN_KEY_CHECKS=0;");
			s.executeUpdate("TRUNCATE matches;");
			s.executeUpdate("TRUNCATE player;");
			s.executeUpdate("SET FOREIGN_KEY_CHECKS=1;");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
